package cn.jimmie.learn.viewArt.refreshview.core.Listview;

import android.view.View;
import android.widget.AbsListView;
import android.widget.ListView;

import cn.jimmie.learn.viewArt.refreshview.core.ContentView;
import cn.jimmie.learn.viewArt.refreshview.core.RefreshState;

/**
 * function : 适配ListView的下拉刷新和上拉加载
 *
 * <p> Create Time:  2019年04月11日 22:32 </p>
 * <p> @author Jimmie.Qian  </p>
 */
public class ListContentView implements ContentView, AbsListView.OnScrollListener {
    private ListView mView;
    private OnFootLocationListener mListener;
    private RefreshState mStatus;

    private boolean isFooterAdded = false;

    public ListContentView(View view, RefreshState status) {
        this.mView = (ListView) view;
        this.mStatus = status;
        this.mView.setOnScrollListener(this);
    }

    @Override
    public boolean isTopPosition() {
        // mView 不可见, 则可以触发刷新
        if (mView.getVisibility() != View.VISIBLE) {
            return true;
        }

        boolean isTop = false;

        View firstChild = mView.getChildAt(0);
        if (firstChild != null) {
            int firstVisiblePos = mView.getFirstVisiblePosition();
            // 如果首个元素的上边缘，距离父布局值为0，就说明ListView滚动到了最顶部，此时应该允许下拉刷新
            if (firstVisiblePos == 0 && firstChild.getTop() == 0) {
                isTop = true;
            }
        }
        // 如果ListView中没有元素，也应该允许下拉刷新
        else isTop = true;

        return isTop;
    }

    @Override
    public void setFooterVisibility(View view, int visible) {
        if (!mStatus.isFootViewEnable()) {
            removeFootView(view);
            return;
        }

        if (!isFooterAdded) {
            mView.addFooterView(view);
            isFooterAdded = true;
        }
        view.setVisibility(visible);
        if (visible == View.VISIBLE) {
            mView.setFooterDividersEnabled(true);
        } else mView.setFooterDividersEnabled(false);
    }

    @Override
    public void setOnFooterLocationListener(OnFootLocationListener listener) {
        this.mListener = listener;
    }

    @Override
    public void release() {

    }

    @Override
    public void removeFootView(View v) {
        if (isFooterAdded) mView.removeFooterView(v);
    }

    @Override
    public void onScrollStateChanged(AbsListView view, int scrollState) {
        if (!mStatus.isFootViewEnable()) return;
        if (scrollState != SCROLL_STATE_IDLE) return;

        int firstVisible = view.getFirstVisiblePosition();
        int lastVisible = view.getLastVisiblePosition();
        int total = view.getCount();

        // 1-2 没有占满一屏, 则不支持 上拉加载
        // 2 拉到最后一项
        // 3 不是出于刷新的某个步骤状态
        // 4 上拉结果不是 END
        // 5 列表不能为空
        if (firstVisible != 0 &&
                lastVisible + 1 == total &&
                mStatus.getLoadState() == RefreshState.LoadState.IDLE &&
                mStatus.getResultState() != RefreshState.ResultState.END &&
                total > 1) {

            mStatus.setLoadState(RefreshState.LoadState.PULLING);
            if (mListener != null) mListener.onFooterRefresh();
        }
    }

    @Override
    public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) {
    }
}
